<?php
class Gold_Productprint_IndexController extends Mage_Core_Controller_Front_Action{

   public function indexAction(){
      $this->loadLayout();
      $catalog = $this->getLayout()->createBlock('productprint/catalog')->setTemplate('productprint/catalog.phtml'); ; 
      $this->getLayout()->getBlock('content')->append($catalog);
      $this->renderLayout();
    }
    public function viewAction(){
        $category_id = $this->getRequest()->getParam('id');
        if(!$category_id) $html =  'url参数出错';

        if($category_id){
            $category = Mage::getModel('catalog/category')->load($category_id); 
            if(!$category->getId()){

                $html =  '找不到分类';
            }else{
                $help = Mage::helper('core');
                $baseDir = Mage::getUrl().'media/'; 
                $productCollection = $category->getProductCollection();
                //$html ='<h1>'.$category->getName().'</h1>';
                 $html = '<table width="100%" cellspacing="0" cellpadding="0" border="0" align="center">
		<tbody><tr>
			<td align="center"><font style="font-size:20">Price List for Products</font></td>
		</tr>
		<tr>
			<td align="center"><b>Generated on '.Mage::app()->getLocale()->date()->toString().'</b></td>
		</tr>
		<tr>
			<td align="center">* Please click on \'File\' -&gt; \'Save As\' to save the Price List</td>
		</tr>
		</tbody></table>';
                $html .= '<table style="border-collapse:collapse;border:none;width:1000;margin:0 auto;text-align:center">';
                $html .='<tr><th style="border:1px solid #ccc">Sku</th><th style="border:1px solid #ccc">Image</th><th style="border:1px solid #ccc">Name</th><th style="border:1px solid #ccc">Unit Price</th></tr>';
                foreach($productCollection as $product){
                    $product = Mage::getModel('catalog/product')->load($product->getId());
                    $html .='<tr>';
                    $html .='<td style="border:1px solid #ccc">'.$product->getSku().'</td>';
                    $image =  $product->getSmallImage()?'<img width="100" src="'.Mage::helper('catalog/image')->init($product, 'small_image',$product->getSmallImage())->resize(100).'" />':'';
                    $html .='<td style="border:1px solid #ccc">'.$image.'</td>';
                    $html .='<td style="border:1px solid #ccc"><a target="_blank" href="'.$product->getProductUrl().'">'.$product->getName().'</a></td>';
                    $html .='<td style="border:1px solid #ccc">'.$help->currency($product->getPrice()).'</td>';
                 
                    $html .='</tr>';
                }
                $html .= '</table>';
            }

        }
        $html = '<div style="text-align:center">'.$html.'</div>';
        $this->getResponse()->setBody($html);
    }
}    
